#pragma once
#include <deque>
#include <math.h>
#include <iostream>
#include "Bit.h"

/**
*	Class Bitstr is used to represent any binary number with bore than one bit. 
*	The class is implemented as a deque containing Bit elements. 
*	Methods in this class include methods for adding two strings together and two complement to represent negative values.
*/

using namespace std;
class Bitstr : public deque<Bit>
{
public:

	Bitstr(void)
	{
	}

	~Bitstr(void)
	{
	}

	void print()
	{
		for( int i = 0; i < this->size(); i++)
		{
			cout << (*this)[i].GetValue() << " ";
		}
		cout << endl;
	}

	void addstr( Bitstr bstr, Bitstr *sumstr )
	{

		Bitstr::iterator it;
		sumstr->clear();

		Bit *sum = new Bit();
		Bit *cu = new Bit();
		Bit ci;
		Bit padding('0');

		// Check if one Bitstr is larger then the other, and insert padding before MSB

		if( this->size() > bstr.size() )
		{
			for( int i = bstr.size(); i < this->size(); i++ )
			{
				bstr.insert(bstr.begin(), padding);
			}
		}
		if( bstr.size() > this->size() )
		{
			for( int i = this->size(); i < bstr.size(); i++ )
			{
				this->insert(this->begin(), padding);
			}
		}

		for( int i = this->size()-1; i >= 0; i-- )
		{
			ci = *cu;
			(*this)[i].add(bstr[i],ci,sum,cu);
			sumstr->push_front(*sum);
		}
		sumstr->push_front(*cu);
		
	}

	Bitstr * twocomp( )
	{
		for( auto &it : *this )
		{
			if( it.GetValue() == '1' )
				it.SetValue('0');
			else
				it.SetValue('1');
		}

		/*for( int i = 0; i < (*this).size() -1; i++ )
		{
			if( (*this)[i].GetValue() == '1' )
				(*this)[i].SetValue('0');
			else
				(*this)[i].SetValue('1');
		}*/

		Bit temp('1');
		Bitstr tempstr;
		tempstr.push_front(temp);

		Bitstr *returnStr = new Bitstr();

		this->addstr(tempstr, returnStr );

		return returnStr;
		
	}
};

